package com.example.knowledgeget1.ChatCommon.Repository;

import java.util.List;
import java.util.Optional;

import com.example.knowledgeget1.ChatCommon.Entity.NodeType;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public interface NodeTypeRepository extends JpaRepository<NodeType, String> {
    NodeType findByName(String name);
    NodeType save(NodeType nodeType);

    // 根据其他条件定义更多查询方法
    @Transactional
    @Modifying
    @Query("SELECT s FROM NodeType s WHERE s.isBuild = false")
    List<NodeType> findAllNotBuild();

    //根据id修改知识点为已构筑
    @Transactional
    @Modifying
    @Query("UPDATE NodeType s SET s.isBuild = true WHERE s.id = ?1")
    void updateIsBuildById(String id);

    // 根据id修改知识点为未构筑
    @Transactional
    @Modifying
    @Query("UPDATE NodeType s SET s.isBuild = false WHERE s.id = ?1")
    void updateIsNotBuildById(String id);

    //查找所有知识点
    @NotNull
    @Transactional
    @Modifying
    @Query("SELECT s FROM NodeType s")
    List<NodeType> findAll();

    //查找序号最大的知识点
    @Query("SELECT n FROM NodeType n ORDER BY n.id DESC LIMIT 1")
    Optional<NodeType> findTopByOrderByIdDesc();

    List<NodeType> findByIsReleaseTrueAndIdIn(List<Long> ids);

    List<NodeType> findByIdIn(List<Long> knowIds);

    List<NodeType> findIdsByNameIn(List<String> names);

    void deleteByName(String name);
}
